﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Negocios;
using Microsoft.Reporting.WinForms;

namespace MiraPark
{
    public partial class FormCadastroCor : Form
    {
        private string operacao = "";

        public FormCadastroCor()
        {
            InitializeComponent();
        }

        private void FormCadastroCor_Load(object sender, EventArgs e)
        {
            //Popular a lista de cores cadastrados
            this.dataGridViewCores.DataSource = ModelBO.getCores();
            setupButtonsControls();
        }

        private void dataGridViewCores_SelectionChanged(object sender, EventArgs e)
        {
            setupButtonsControls();
        }

        private void buttonNovo_Click(object sender, EventArgs e)
        {
            this.operacao = "novo";
            this.numericUpDownOrdem.Value = 999;
            setupButtonsControls();
        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.operacao = "cancelar";
            setupButtonsControls();
        }

        private void buttonEditar_Click(object sender, EventArgs e)
        {
            this.operacao = "editar";
            setupButtonsControls();
        }

        private void buttonExcluir_Click(object sender, EventArgs e)
        {
            List<CorVO> corList = (List<CorVO>)this.dataGridViewCores.DataSource;
            CorVO cor = corList[this.dataGridViewCores.CurrentRow.Index];

            if (!ModelBO.readyToDeleteCor(cor.idCor))
            {
                MessageBox.Show(this, "Cor não pode ser excluída pois já está associada a veículos", "Aviso");
                return;
            }

            this.operacao = "excluir";

            DialogResult dr = MessageBox.Show(this, "Confirma a exclusão da cor " + cor.nome, "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString().Equals("Yes"))
            {
                ModelBO.delCor(cor);

                int nRow = this.dataGridViewCores.CurrentRow.Index;
                this.dataGridViewCores.DataSource = ModelBO.getCores();
                if (this.dataGridViewCores.Rows.Count > 0)
                {
                    dataGridViewCores.CurrentCell = dataGridViewCores.Rows[Math.Min(nRow, this.dataGridViewCores.Rows.Count - 1)].Cells[1];
                    dataGridViewCores.Rows[Math.Min(nRow, this.dataGridViewCores.Rows.Count - 1)].Selected = true;
                }

                setupButtonsControls();
            }
        }

        private void buttonSalvar_Click(object sender, EventArgs e)
        {
            String erros = "";
            if (this.textBoxCor.Text.Trim().Length == 0)
                erros = erros + "Nome da cor deve ser informado.\r\n";
            if (this.textBoxAlpha.Text.Trim().Length == 0)
                erros = erros + "Cor deve ser informado.\r\n";
            if (erros.Length > 0)
            {
                MessageBox.Show(this, erros, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            CorVO cor = null;

            if (this.operacao.Equals("novo"))
            {

                cor = CorVO.CreateCorVO();
                cor.nome = this.textBoxCor.Text;
                cor.a = int.Parse(this.textBoxAlpha.Text);
                cor.r = int.Parse(this.textBoxRed.Text);
                cor.g = int.Parse(this.textBoxGreen.Text);
                cor.b = int.Parse(this.textBoxBlue.Text);
                cor.ordem = (int)this.numericUpDownOrdem.Value;

                cor.idCor = (int)ModelBO.newCor(cor);
            }
            if (this.operacao.Equals("editar"))
            {
                List<CorVO> corList = (List<CorVO>)this.dataGridViewCores.DataSource;
                cor = corList[this.dataGridViewCores.CurrentRow.Index];

                cor.nome = this.textBoxCor.Text;
                cor.a = int.Parse(this.textBoxAlpha.Text);
                cor.r = int.Parse(this.textBoxRed.Text);
                cor.g = int.Parse(this.textBoxGreen.Text);
                cor.b = int.Parse(this.textBoxBlue.Text);
                cor.ordem = (int)this.numericUpDownOrdem.Value;

                ModelBO.setCor(cor);
            }
            this.operacao = "salvar";

            List<CorVO> listCorVOs = ModelBO.getCores();
            dataGridViewCores.DataSource = listCorVOs;
            int nRow = listCorVOs.FindIndex(a => a.idCor == cor.idCor);
            dataGridViewCores.CurrentCell = dataGridViewCores.Rows[nRow].Cells[1];
            dataGridViewCores.Rows[nRow].Selected = true;

            setupButtonsControls();
        }


        private void setupButtonsControls()
        {
            bool haRows = this.dataGridViewCores.Rows.Count > 0 && this.dataGridViewCores.CurrentRow!=null;
            bool novo = this.operacao.Equals("novo");
            bool edit = this.operacao.Equals("editar");
            bool canc = this.operacao.Equals("cancelar");

            this.buttonCancelar.Enabled = novo || edit;
            this.buttonNovo.Enabled = !novo && !edit;
            this.buttonEditar.Enabled = haRows && !novo && !edit;
            this.buttonExcluir.Enabled = haRows && !novo && !edit;
            this.buttonSalvar.Enabled = novo || edit;
            this.buttonColor.Enabled = novo || edit;

            this.textBoxCor.Enabled = novo || edit;
            this.textBoxAlpha.Enabled = novo || edit;
            this.numericUpDownOrdem.Enabled = novo || edit;

            this.dataGridViewCores.Enabled = !novo && !edit;

            if (!haRows || novo)
            {
                this.textBoxId.Text = "";
                this.textBoxCor.Text = "";
                this.textBoxAlpha.Text = "";
                this.textBoxRed.Text = "";
                this.textBoxGreen.Text = "";
                this.textBoxBlue.Text = "";
                this.numericUpDownOrdem.Value = 999;

                this.ovalShape1.FillColor = Color.White;
            }
            else {
                List<CorVO> corList = (List<CorVO>)this.dataGridViewCores.DataSource;
                CorVO cor = corList[this.dataGridViewCores.CurrentRow.Index];
                this.textBoxId.Text = cor.idCor.ToString();
                this.textBoxCor.Text = cor.nome;
                this.textBoxAlpha.Text = cor.a.ToString();
                this.textBoxRed.Text = cor.r.ToString();
                this.textBoxGreen.Text = cor.g.ToString();
                this.textBoxBlue.Text = cor.b.ToString();
                this.ovalShape1.FillColor = Color.FromArgb(cor.a, cor.r, cor.g, cor.b);
                this.numericUpDownOrdem.Value = cor.ordem;

                this.dataGridViewCores.CurrentRow.Cells["rgb"].Style.BackColor = Color.FromArgb(cor.a, cor.r, cor.g, cor.b);
            }
            if (novo || edit){
                this.textBoxCor.Focus();
            }
        }

        private void buttonColor_Click(object sender, EventArgs e)
        {

            ColorDialog MyDialog = new ColorDialog();
            // Allow the user from selecting a custom color.
            MyDialog.AllowFullOpen = true ;
            // Allows the user to get help. (The default is false.)
            MyDialog.ShowHelp = true ;
            // Sets the initial color select to the current text color.
            MyDialog.Color = textBoxCor.ForeColor ;

            // Update the text box color if the user clicks OK 
            if (MyDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxAlpha.Text = MyDialog.Color.A.ToString();
                textBoxRed.Text = MyDialog.Color.R.ToString();
                textBoxGreen.Text = MyDialog.Color.G.ToString();
                textBoxBlue.Text = MyDialog.Color.B.ToString();
                this.ovalShape1.FillColor = MyDialog.Color;
            }

        }

        private void dataGridViewCores_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            foreach (DataGridViewRow dgvr in dgv.Rows)
            {
                dgvr.Cells["rgb"].Style.BackColor = Color.FromArgb((int)dgvr.Cells["a"].Value, (int)dgvr.Cells["r"].Value, (int)dgvr.Cells["g"].Value, (int)dgvr.Cells["b"].Value);
            }
        }

        private void buttonListagem_Click(object sender, EventArgs e)
        {
            FormReportCor formx = new FormReportCor();
            formx.MdiParent = this.MdiParent;
            formx.Show();
        }

     }
}
